<?php

/**
 * Twoir CMS系统 twCMS
 *
 * 商业版授权请访问axzz.cn查看。
 * 
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.01
*/

namespace App\Services\User;

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

/**
 * 用户验证类
 */
class ValidatorUser
{
    /**
    * 用户注册
    *
    * @param array $data
    * @throws ValidationException
    * @return array
    */
    public function register(array $data): array
    {
        $validator = Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed']
        ]);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

        return $validator->validated();
    }

    /**
    * 用户登陆
    *
    * @param array $data
    * @throws ValidationException
    * @return array
    */
    public function login(array $data): array
    {
        $validator = Validator::make($data, [
            'email' => ['required', 'email'],
            'password' => ['required', 'string'],
        ]);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

        return $validator->validated();
    }

    /**
     * 修改密码
     * 
     * @param array $data
     * @throws ValidationException
     * @return array
     */
    public function changePassword(array $data): array
    {
        $validator = Validator::make($data, [
            'current_password' => ['required', 'string'],
            'new_password' => ['required', 'string', 'min:6', 'confirmed']
        ]);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

        return $validator->validated();
    }
}